#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Copyright (c) 2025 粤港澳大湾区（广东）国创中心

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <https://www.gnu.org/licenses/>.

File: onCataegoryModel.py
Desc: Model pannel in the application.
Author: xumanjia
Date: 2025-08-01
"""
import json
from PyQt5.QtCore import Qt
from scripts.Model.pyncti_display_all import pyncti_display_all
from scripts.Model.pyncti_hide_selected import pyncti_hide_selected
from scripts.Model.pyncti_showonly_selected import pyncti_showonly_selected
from scripts.Model.pyncti_zoom_view_xyz import pyncti_zoom_view_xyz
from scripts.Model.pyncti_zoom import pyncti_zoom
from scripts.Model.pyncti_visual_mode import pyncti_visual_mode
from scripts.Model.pyncti_select_mode import pyncti_select_mode
from scripts.Model.pyncti_select_all import pyncti_select_all
from scripts.Model.pyncti_search_loop_face import pyncti_search_loop_face
from scripts.Model.pyncti_search_connect_face import pyncti_search_connect_face
from scripts.Model.pyncti_search_noplane_connect_face import pyncti_search_noplane_connect_face
from scripts.Model.pyncti_measure_length import pyncti_measure_length
from scripts.Model.pyncti_measure_point import pyncti_measure_point
from scripts.Model.pyncti_measure_midpoint import pyncti_measure_midpoint
from scripts.Model.pyncti_measure_circle_property import pyncti_measure_circle_property
from scripts.Model.pyncti_measure_vector import pyncti_measure_vector
from scripts.Model.pyncti_measure_angle import pyncti_measure_angle

def on_display_all(self):
    if False == hasattr(self, 'doc'):
        return   
    pyncti_display_all(self.NCTI,self.doc)
    
def on_hide_selected(self):
    if False == hasattr(self, 'doc'):
        return 
    pyncti_hide_selected(self.NCTI,self.doc)
    
def on_diaplay_selected_only(self):
    if False == hasattr(self, 'doc'):
        return   
    pyncti_showonly_selected(self.NCTI,self.doc)
    
def on_zoom_view(self, xyz):
    if False == hasattr(self, 'doc'):
        return
    pyncti_zoom_view_xyz(self.doc,xyz)

def on_zoom_button_click(self):
    if False == hasattr(self, 'doc'):
        return
    pyncti_zoom(self.NCTI,self.doc)

def on_visual_checkbox_body(self, state):
    if False == hasattr(self, 'doc'):
        return
    if state == Qt.Checked:
        pyncti_visual_mode(self.doc,1, 1, 1)
        self.visualmode_face.setChecked(True)
        self.visualmode_face.setEnabled(False)

        self.visualmode_line.setChecked(True)
        self.visualmode_line.setEnabled(False)

        self.visualmode_vertex.setChecked(True)
        self.visualmode_vertex.setEnabled(False)
    else:
        self.visualmode_face.setEnabled(True)
        self.visualmode_line.setEnabled(True)
        self.visualmode_vertex.setEnabled(True)

def on_visual_checkbox_face(self, state):
    if False == hasattr(self, 'doc'):
        return
    ischecked_face = 1 if state == Qt.Checked else 0
    ischecked_line = 1 if self.visualmode_line.isChecked() else 0
    ischecked_vertex = 1 if self.visualmode_vertex.isChecked() else 0
    pyncti_visual_mode(self.doc,ischecked_face, ischecked_line, ischecked_vertex)

def on_visual_checkbox_line(self, state):
    if False == hasattr(self, 'doc'):
        return
    ischecked_line = 1 if state == Qt.Checked else 0
    ischecked_face = 1 if self.visualmode_face.isChecked() else 0
    ischecked_vertex = 1 if self.visualmode_vertex.isChecked() else 0
    pyncti_visual_mode(self.doc,ischecked_face, ischecked_line, ischecked_vertex)

def on_visual_checkbox_vertex(self, state):
    if False == hasattr(self, 'doc'):
        return
    ischecked_vertex = 1 if state == Qt.Checked else 0
    ischecked_line = 1 if self.visualmode_line.isChecked() else 0
    ischecked_face = 1 if self.visualmode_face.isChecked() else 0
    pyncti_visual_mode(self.doc, ischecked_face, ischecked_line, ischecked_vertex)

def on_sel_checkbox_body(self, state):
    if False == hasattr(self, 'doc'):
        return
    pyncti_select_mode(self.NCTI, self.doc, 0, state == Qt.Checked)
    if state == Qt.Checked:
        self.selmode_face.setChecked(True)
        self.selmode_line.setChecked(True)
        self.selmode_vertex.setChecked(True)


def on_sel_checkbox_face(self, state):
    if False == hasattr(self, 'doc'):
        return
    pyncti_select_mode(self.NCTI, self.doc, 1, state == Qt.Checked)
    if state != Qt.Checked:
        self.selmode_body.setChecked(False)

def on_sel_checkbox_line(self, state):
    if False == hasattr(self, 'doc'):
        return
    pyncti_select_mode(self.NCTI, self.doc, 2, state == Qt.Checked)
    if state != Qt.Checked:
        self.selmode_body.setChecked(False)

def on_sel_checkbox_vertex(self, state):
    if False == hasattr(self, 'doc'):
        return
    pyncti_select_mode(self.NCTI, self.doc, 3, state == Qt.Checked)
    if state != Qt.Checked:
        self.selmode_body.setChecked(False)

def on_allsel_button_click(self):
    if False == hasattr(self, 'doc'):
        return
    if self.selmode_body.isChecked():
        pyncti_select_all(self.NCTI, self.doc)

def on_airecog_button_click(self):
    if False == hasattr(self, 'doc'):
        return

def on_searchloop_button_click(self):
    if False == hasattr(self, 'doc'):
        return
    pyncti_search_loop_face(self.NCTI, self.doc)

def on_searchface_button_click(self):
    if False == hasattr(self, 'doc'):
        return
    pyncti_search_connect_face(self.NCTI, self.doc)

def on_searchnoplaneface_button_click(self):
    if False == hasattr(self, 'doc'):
        return
    pyncti_search_noplane_connect_face(self.NCTI, self.doc)

def on_measure_getpt_click(self):
    if False == hasattr(self, 'doc'):
        return
    pt = pyncti_measure_point(self.NCTI, self.doc)
    print(f"pt: ({pt.X}, {pt.Y}, {pt.Z})")

def on_measure_getlen_click(self):
    if False == hasattr(self, 'doc'):
        return
    len = pyncti_measure_length(self.NCTI, self.doc)
    data = extract_fields(len)
    print(data)

def on_measure_getmidpt_click(self):
    if False == hasattr(self, 'doc'):
        return
    pt = pyncti_measure_midpoint(self.NCTI, self.doc)
    print(f"pt: ({pt.X}, {pt.Y}, {pt.Z})")

def extract_fields(s):
    try:
        s = str(s)
        json_str = s.replace("'", '"')
        data = json.loads(json_str)
        return data[0]  # 返回第一个字典对象
    except Exception as e:
        print(f"解析错误: {e}")
        return None

def on_measure_getcenter_click(self):
    if False == hasattr(self, 'doc'):
        return
    property = pyncti_measure_circle_property(self.NCTI, self.doc)
    data = extract_fields(property)
    x, y, z = data['pt']
    print(f"center: ({x}, {y}, {z})")

def on_measure_getradius_click(self):
    if False == hasattr(self, 'doc'):
        return
    property = pyncti_measure_circle_property(self.NCTI, self.doc)
    data = extract_fields(property)
    print(f"radius: {data['radius']}")

def on_getvec_button_click(self):
    if False == hasattr(self, 'doc'):
        return
    property = pyncti_measure_vector(self.NCTI, self.doc)
    print(f"vector: ({property})")

def on_getangle_button_click(self):
    if False == hasattr(self, 'doc'):
        return
    property = pyncti_measure_angle(self.NCTI, self.doc)
    print(f"angle: {property}")
